﻿Imports System.Data
Imports System.Data.SqlClient
Public Class citaForm

#Region "Atributos"
    Dim paciente As Paciente
    Dim modificar As Boolean = False
    Dim citaSeleccionada As cita
    Dim ultimaCita As cita
    Dim tablaCitasPasadas As DataTable
    Dim tablaCitasFuturas As DataTable
#End Region
#Region "Propiedades"

#End Region
#Region "Metodos"
#Region "Constructores"
    Public Sub New(ByVal paciente As Paciente)
        Me.paciente = paciente
        cargarUltimaCita()
        InitializeComponent()
        Me.tablaCitasPasadas = New DataTable("resultados")
        Me.tablaCitasPasadas.Columns.Add("id")
        Me.tablaCitasPasadas.Columns.Add("Tratamiento")
        Me.tablaCitasPasadas.Columns.Add("fecha")

        Me.tablaCitasFuturas = New DataTable("Citas Futuras")
        Me.tablaCitasFuturas.Columns.Add("id")
        Me.tablaCitasFuturas.Columns.Add("Tratamiento")
        Me.tablaCitasFuturas.Columns.Add("fecha")
    End Sub
#End Region
#Region "GUI"
    Private Sub btnCrearCita_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCrearCita.Click
        If modificar = False Then
            If ctrFecha.Value >= Date.Now Then
                Dim cita As New cita(txtTratamiento.Text, ctrFecha.Value, Me.paciente)
                cleanControls()
            Else
                MsgBox("Error: La fecha seleccionada es menor a la actual" & vbNewLine & _
                       "por favor revise el valor selecionado", MsgBoxStyle.Critical, "Error")
                ctrFecha.Focus()
            End If
        Else
            Me.citaSeleccionada.modificar(Me.txtTratamiento.Text, Me.ctrFecha.Value)
            cleanControls()
        End If
        obtenerCitasFuturas()
        ObtenerCitasPasadas()

    End Sub
    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        txtTratamiento.Text = ""
        ctrFecha.Value = Date.Now
        TabControl1.SelectedTab = Me.tabView
    End Sub

    Private Sub cleanControls()
        txtTratamiento.Text = ""
        ctrFecha.Value = Date.Now
    End Sub

    Private Sub citaForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.Owner.Enabled = True
    End Sub
    Private Sub citaForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.lblNombrePaciente.Text = Me.paciente.NombreCompleto
        Me.lblTratamientoAnterior.Text = Me.ultimaCita.Tratamiento
        Me.lblUltmaVisita.Text = Me.ultimaCita.Fecha.ToString("dd/MM/yy")
        ObtenerCitasPasadas()
        Me.CitasPasadas.DataSource = tablaCitasPasadas
        obtenerCitasFuturas()
        Me.CitasFuturas.DataSource = tablaCitasFuturas
    End Sub
#End Region
#Region "Logica De Negocio"
    Private Sub cargarUltimaCita()
        Dim con As New Conexion
        con.Conectar()
        Dim comando As New SqlCommand("cargar_ultima_cita", con.SqlCon)
        comando.CommandType = CommandType.StoredProcedure
        comando.Parameters.AddWithValue("@id_paciente", Me.paciente.Id_cliente)

        Dim dr As SqlDataReader
        dr = comando.ExecuteReader

        While dr.Read()
            ultimaCita = New cita(Int32.Parse(dr.Item(0).ToString))
        End While
        con.Desconectar()
    End Sub
    Private Sub cambioStatus()
        If modificar = True Then
            txtTratamiento.Text = citaSeleccionada.Tratamiento
            ctrFecha.Value = citaSeleccionada.Fecha
            btnCrearCita.Text = "Modificar Cita"
            tabAddMod.Text = "Modificar"
        Else
            cleanControls()
            tabAddMod.Text = "Agregar"
            btnCrearCita.Text = "Agregar Cita"
        End If
    End Sub
    Private Sub ObtenerCitasPasadas()
        Me.tablaCitasPasadas.Clear()
        Dim cita As cita
        Dim fila As DataRow = tablaCitasPasadas.NewRow
        Dim con As New Conexion
        con.Conectar()
        Dim comando As New SqlCommand("obtener_citas_pasadas", con.SqlCon)
        comando.CommandType = CommandType.StoredProcedure
        comando.Parameters.AddWithValue("@id_paciente", Me.paciente.Id_cliente)
        Dim dr As SqlDataReader = comando.ExecuteReader

        While dr.Read()
            cita = New cita(Int32.Parse(dr.Item(0).ToString))
            rellenarFilaCitasPasadas(cita)
        End While
        con.Desconectar()
    End Sub
    Private Sub obtenerCitasFuturas()
        Me.tablaCitasFuturas.Clear()
        Dim cita As cita
        Dim fila As DataRow = tablaCitasPasadas.NewRow
        Dim con As New Conexion
        con.Conectar()
        Dim comando As New SqlCommand("obtener_citas_futuras", con.SqlCon)
        comando.CommandType = CommandType.StoredProcedure
        comando.Parameters.AddWithValue("@id_paciente", Me.paciente.Id_cliente)
        Dim dr As SqlDataReader = comando.ExecuteReader

        While dr.Read()
            cita = New cita(Int32.Parse(dr.Item(0).ToString))
            rellenarFilaCitasFuturas(cita)
        End While
        con.Desconectar()
    End Sub
    Private Sub rellenarFilaCitasPasadas(ByVal cita As cita)
        Dim fila As DataRow = tablaCitasPasadas.NewRow
        fila.Item("id") = cita.id
        fila.Item("Tratamiento") = cita.Tratamiento
        fila.Item("fecha") = cita.Fecha.ToString("dd/MM/yy")
        tablaCitasPasadas.Rows.Add(fila)
    End Sub
    Private Sub rellenarFilaCitasFuturas(ByVal cita As cita)
        Dim fila As DataRow = tablaCitasFuturas.NewRow
        fila.Item("id") = cita.id
        fila.Item("Tratamiento") = cita.Tratamiento
        fila.Item("fecha") = cita.Fecha.ToString("dd/MM/yy")
        tablaCitasFuturas.Rows.Add(fila)
    End Sub
#End Region
#End Region

    Private Sub ModificarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ModificarToolStripMenuItem.Click
        Me.modificar = True
        cambioStatus()
        Me.TabControl1.SelectedTab = Me.tabAddMod
    End Sub
End Class